# -*- mode: python ; coding: utf-8 -*-

import os
import glob
import platform

# 查找Azure语音SDK的路径和二进制文件
system = platform.system().lower()
if system == 'windows':
    binary_pattern = "*.dll"
elif system == 'linux':
    binary_pattern = "*.so"
else:  # macOS
    binary_pattern = "*.dylib"

# 使用更可靠的方法查找Azure语音SDK路径
try:
    import azure.cognitiveservices.speech as speechsdk
    azure_speech_path = os.path.dirname(speechsdk.__file__)
except ImportError:
    # 备用方法
    import sys
    if system == 'windows':
        azure_speech_path = os.path.join(os.environ.get('CONDA_PREFIX', ''), 'Lib', 'site-packages', 'azure', 'cognitiveservices', 'speech')
    else:  # Linux or macOS
        # 对于conda环境中的Linux系统
        conda_prefix = os.environ.get('CONDA_PREFIX', '')
        if conda_prefix:
            azure_speech_path = os.path.join(conda_prefix, 'lib', 'python3*', 'site-packages', 'azure', 'cognitiveservices', 'speech')
        else:
            # 其他情况
            azure_speech_path = os.path.join(os.path.dirname(sys.executable), '..', 'lib', 'python*', 'site-packages', 'azure', 'cognitiveservices', 'speech')

# 获取所有二进制文件
binary_files = []
if os.path.exists(azure_speech_path):
    binary_files = glob.glob(os.path.join(azure_speech_path, binary_pattern))
else:
    # 如果上述方法失败，则尝试通过glob模式匹配
    import sys
    site_packages = os.path.join(os.path.dirname(sys.executable), 'site-packages')
    azure_speech_glob = os.path.join(site_packages, 'azure', 'cognitiveservices', 'speech', binary_pattern)
    binary_files = glob.glob(azure_speech_glob)
    
    # 如果仍然找不到，尝试递归搜索
    if not binary_files:
        import sys
        base_search_path = os.path.dirname(sys.executable)
        for root, dirs, files in os.walk(base_search_path):
            if 'azure' in dirs and 'cognitiveservices' in dirs:
                azure_path = os.path.join(root, 'azure', 'cognitiveservices', 'speech')
                if os.path.exists(azure_path):
                    binary_files = glob.glob(os.path.join(azure_path, binary_pattern))
                    break

# 构建二进制文件列表
binaries = [(binary, 'azure/cognitiveservices/speech') for binary in binary_files]

# 添加特定需要的数据文件
datas = []
data_files = []
if os.path.exists(azure_speech_path):
    # 添加所有数据文件
    for root, dirs, files in os.walk(azure_speech_path):
        for file in files:
            if not file.endswith(('.py', '.pyc', '.pyo', '__pycache__', binary_pattern[1:])):
                full_path = os.path.join(root, file)
                rel_dir = os.path.relpath(root, os.path.dirname(azure_speech_path))
                data_files.append((full_path, os.path.join(rel_dir, 'azure/cognitiveservices/speech')))

datas.extend(data_files)

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=binaries,
    datas=datas,
    hiddenimports=['azure.cognitiveservices.speech'],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)

# 解决GLIBC兼容性问题 - 排除libpython相关文件
# 这些文件可能会导致在不同版本的Linux系统间出现兼容性问题
exclude_binaries = []
for binary in a.binaries:
    # 检查是否为libpython相关的二进制文件
    if isinstance(binary[0], str) and 'libpython' in binary[0]:
        exclude_binaries.append(binary[0])

# 从binaries中移除这些可能导致问题的文件
a.binaries = [binary for binary in a.binaries if binary[0] not in exclude_binaries]

# 同样从datas中排除libpython相关文件
exclude_datas = []
for data in a.datas:
    if isinstance(data[0], str) and 'libpython' in data[0]:
        exclude_datas.append(data[0])

# 从datas中移除这些可能导致问题的文件
a.datas = [data for data in a.datas if data[0] not in exclude_datas]

pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.datas,
    [],
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)